﻿Imports System.Data.SqlClient

Public Class FrmNhapThongTin
    Public m_tennhap As String
    Private Sub NapLoaiDat2()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select kyhieu, tenld from LOAIDAT", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbLD2.DisplayMember = "kyhieu"
        CmbLD2.ValueMember = "kyhieu"
        CmbLD2.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapLoaiDat1()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select kyhieu, tenld from LOAIDAT", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbLD1.DisplayMember = "kyhieu"
        CmbLD1.ValueMember = "kyhieu"
        CmbLD1.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapDoiTuong()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMDOITUONG", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbDoiTuong.DisplayMember = "TenDoiTuong"
        CmbDoiTuong.ValueMember = "TenDoiTuong"
        CmbDoiTuong.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapLoaiBienDong()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from LOAIBD", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbLoaiBD.DisplayMember = "TenLoaiBD"
        CmbLoaiBD.ValueMember = "maBD"
        CmbLoaiBD.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapDonViHuyen()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from TENHUYEN", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbHuyen.DisplayMember = "T_huyen"
        CmbHuyen.ValueMember = "maH"
        CmbHuyen.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapLoaiTaiLieu()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from DMTaiLieu", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbLoaiTaiLieu.DisplayMember = "TenLoaiTaiLieu"
        CmbLoaiTaiLieu.ValueMember = "maLoaiTaiLieuID"
        CmbLoaiTaiLieu.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub Tat_chuc_nang()
        Me.ChuSD1.Enabled = False
        Me.TextDiaChiTruoc.Enabled = False
        Me.CmbHuyen.Enabled = False
        Me.CmbXa.Enabled = False
        Me.TextToBD1.Enabled = False
        Me.TextSoThua1.Enabled = False
        Me.TextDienTich1.Enabled = False
        Me.CmbLD1.Enabled = False
        Me.TextSoThua2.Enabled = False
        Me.TextDienTich2.Enabled = False
        Me.CmbLD2.Enabled = False
        Me.TextNoiDungBD.Enabled = False

        Me.DataGridViewBD.Enabled = False
        Me.DateNgayBD.Enabled = False
        Me.ButtonLuu.Enabled = False
        Me.CmbLoaiBD.Enabled = False
        Me.TextBoxGio.Enabled = False
        Me.TruongHopBD.Enabled = False
        Me.CmbDoiTuong.Enabled = False
        Me.CmbLoaiTaiLieu.Enabled = False
    End Sub
    Private Sub Bat_chuc_nang()
        Me.ChuSD1.Enabled = True
        Me.TextDiaChiTruoc.Enabled = True
        Me.CmbHuyen.Enabled = True
        Me.CmbXa.Enabled = True
        Me.TextToBD1.Enabled = True
        Me.TextSoThua1.Enabled = True
        Me.TextDienTich1.Enabled = True
        Me.CmbLD1.Enabled = True
        Me.CmbLD2.Enabled = True
        Me.TextNoiDungBD.Enabled = True
        Me.DataGridViewBD.Enabled = True
        Me.DateNgayBD.Enabled = True
        Me.ButtonLuu.Enabled = True
        Me.CmbLoaiBD.Enabled = True
        Me.TextBoxGio.Enabled = True
        Me.TruongHopBD.Enabled = True
        Me.CmbDoiTuong.Enabled = True
        Me.CmbLoaiTaiLieu.Enabled = True
    End Sub
    Private Sub FrmNhapThongTin_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        NapDonViHuyen()
        NapLoaiDat1()
        NapLoaiDat2()
        NapLoaiBienDong()
        NapDoiTuong()
        NapLoaiTaiLieu()
        Tat_chuc_nang()
        DateNgayBD.Text = Date.Today()
        ' dtNgayNhan.Text = Date.Today()
        DataGridViewNoiDungBienDong.RowsDefaultCellStyle.BackColor = Color.LightBlue
        DataGridViewNoiDungBienDong.AlternatingRowsDefaultCellStyle.BackColor = Color.Bisque
        DataGridViewTenChu.RowsDefaultCellStyle.BackColor = Color.LightBlue
        DataGridViewTenChu.AlternatingRowsDefaultCellStyle.BackColor = Color.Azure
        DataGridViewBD.RowsDefaultCellStyle.BackColor = Color.Beige
        DataGridViewBD.AlternatingRowsDefaultCellStyle.BackColor = Color.Azure
    End Sub
    Private Sub CmbHuyen_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmbHuyen.SelectedIndexChanged
        ' load ten huyen
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand(" select * from TenXa where mahuyen = '" & CmbHuyen.SelectedValue.ToString & "' ", ob_cnn)
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        CmbXa.DisplayMember = "TenXa"
        CmbXa.ValueMember = "maxa"
        CmbXa.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub ChuSD1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ChuSD1.TextChanged
        NapDataGridViewtenchu()
    End Sub

    Private Sub TextToBD1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextToBD1.TextChanged
        TextToBD2.Text = TextToBD1.Text
    End Sub

    Private Sub TextSoThua1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextSoThua1.TextChanged
        TextSoThua2.Text = TextSoThua1.Text
    End Sub

    Private Sub TextDienTich1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextDienTich1.TextChanged
        TextDienTich2.Text = TextDienTich1.Text
    End Sub
    Private Sub ButtonThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonThoat.Click
        Dim Msg As String
        Msg = MessageBox.Show("Bạn có thực sự muốn thoát không ?", "Lưu ý", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
        If Msg = Windows.Forms.DialogResult.Yes Then
            Me.Close()
        End If
    End Sub

    Private Sub ButtonLoaiDat1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        FrmLDat.Show()

    End Sub

    Private Sub ButtonLoaiDat2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        FrmLDat.Show()

    End Sub

    Private Sub ButtonLoaiBD_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        FrmLoaiBD.Show()

    End Sub
    Private Sub Them_Du_lieu()
        Try
            Dim NgayNhap As String
            NgayNhap = String.Format("{0:MM/dd/yyyy}", DateNgayBD.Value)
            Dim tennhap As String
            tennhap = ModuleTenDangNhap.m_tennhap
            Dim Sql As String = ""
            Sql = " insert into BIENDONG (loaibdid, NoiDungBD, ngayBD, chusd1,cho_o1,toBD,sothua1,dientich1,loaidat1id,sothua2, dientich2 " &
 " , loaidat2id,diachiHuyenid, diachixaid, tinhtrangid,  ngaynhap, " &
 " nguoinhap, maKhoID, maNganID, maKeID, maPhongID, DoiTuongID, LoaiTaiLieuID, gioBD,  soluongbd ) " &
 " values('" & CmbLoaiBD.SelectedValue.ToString & "', N'" & TextNoiDungBD.Text & "'" &
 " ,'" & NgayNhap & "', N'" & ChuSD1.Text & "',N'" & TextDiaChiTruoc.Text & "'," &
 "  '" & TextToBD1.Text & "','" & TextSoThua1.Text & "','" & TextDienTich1.Text & "','" & CmbLD1.SelectedValue.ToString & "','" & TextSoThua2.Text & "','" & TextDienTich2.Text & "'" &
 ",'" & CmbLD2.SelectedValue.ToString & "','" & CmbHuyen.SelectedValue.ToString & "','" & CmbXa.SelectedValue.ToString & "'," &
 "  '1' ," & Format(Now.ToOADate() - 2) & ", '" & tennhap & "', '0', '0', '0', '0', " &
 " N'" & CmbDoiTuong.SelectedValue.ToString & "', '" & CmbLoaiTaiLieu.SelectedValue.ToString & "', '" & TextBoxGio.Text & "', " &
 "  '" & TruongHopBD.Value.ToString & "' )"

            Dim cmd As New SqlCommand(Sql, ob_cnn)
            ob_cnn.Open()
            cmd.ExecuteNonQuery()
            ob_cnn.Close()
            MsgBox("Thêm thông tin thành công!")
        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub
    Protected Overrides Function ProcessDialogKey(keyData As Keys) As Boolean
        If ButtonLuu.Enabled = True Then
            If (keyData.Equals(Keys.F1)) Then
                Xuly()
            End If
        End If
        Return MyBase.ProcessDialogKey(keyData)
    End Function
    
    Private Sub Xuly()
        ''========= xu ly tên chủ sử dụng
        If ChuSD1.Text = "" Then
            MessageBox.Show("Tên chủ sử dụng không được trống ")
            Return
        End If
        ''============bây lỗi đơn vi ký hợp đồng 
        If TextDiaChiTruoc.Text = "" Then
            MessageBox.Show("Địa chỉ chủ sử dụng không được trống ")
            Return
        End If
        '==========Bẫy lỗi người ký hợp đồng
        If TextToBD1.Text = "" Then
            MessageBox.Show("Tờ bản đồ không hợp lệ")
            Return
        End If
        '============Bẫy lỗi địa chỉ đơn vị ký hợp đồng
        If TextSoThua1.Text = "" Then
            MessageBox.Show("Số hiệu thửa đất không được trống")
            Return
        ElseIf TextSoThua1.Text.StartsWith("-") = True Then
            MessageBox.Show("Số hiệu thửa đất không hợp lệ")
            Return
        End If
        '==========Bẫy nội dung thực hiện


        Try
            If TextDienTich1.Text = "" Then
                MessageBox.Show("Diện tích thửa đất không được trống")
                Return
            ElseIf KiemTraSoAm(TextDienTich1.Text) = False Then
                MessageBox.Show("Diện tích thửa đất không hợp lệ")
                Return
            ElseIf Convert.ToDouble(TextDienTich1.Text) Then
            End If
        Catch ex As Exception
            MessageBox.Show("Diện tích thửa đất không hợp lệ")
            Return
        End Try
        If TextNoiDungBD.Text = "" Then
            MessageBox.Show("Nội dung biến động không được trống")
            Return
        Else
            Them_Du_lieu()
            napDuLieuNhapVaodataGird()
            ob_cnn.Close()
            ' ChuSD1.Text = ""
        End If
        Me.ButtonLuu.Enabled = False
        Me.ButtonNhap.Enabled = True
    End Sub
    Private Sub ButtonLuu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonLuu.Click
        Xuly()
    End Sub

    Private Sub ButtonNhap_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ButtonNhap.Click
        Bat_chuc_nang()
        Me.ButtonNhap.Enabled = False

    End Sub
    Public Sub napDuLieuNhapVaodataGird()
        DataGridViewBD.Rows.Add(DateNgayBD.Text, ChuSD1.Text, TextDiaChiTruoc.Text, TextToBD1.Text, TextSoThua1.Text, TextDienTich1.Text, CmbLD1.SelectedValue.ToString, TextNoiDungBD.Text)
    End Sub

    Private Sub CmbLD1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CmbLD1.SelectedIndexChanged
        CmbLD2.SelectedValue = CmbLD1.SelectedValue
    End Sub

    Private Sub DataGridViewBD_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles DataGridViewBD.RowPostPaint
        Dim strRowNumber As String = (e.RowIndex + 1).ToString
        While (strRowNumber.Length < DataGridViewBD.RowCount.ToString.Length)
            strRowNumber = "0" & strRowNumber
        End While
        Dim Size As SizeF = e.Graphics.MeasureString(strRowNumber, MyBase.Font)
        If DataGridViewBD.RowHeadersWidth < Size.Width + 20 Then DataGridViewBD.RowHeadersWidth = Size.Width + 20
        Dim b As Brush = SystemBrushes.ControlText
        e.Graphics.DrawString(strRowNumber, MyBase.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y +
                              ((e.RowBounds.Height - Size.Height) / 2))

    End Sub
    Private Sub NapDataGridViewTenChu()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand("select  ChuSD1, cho_o1  From  BIENDONG  WHERE chusd1  like N'%" & ChuSD1.Text & "%'" &
                                " and diachixaid = '" & CmbXa.SelectedValue.ToString & "' " &
                                " and diachihuyenid = '" & CmbHuyen.SelectedValue.ToString & "'group by chusd1, cho_o1 ", ob_cnn) '
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        DataGridViewTenChu.DataSource = tbl

        ob_cnn.Close()
    End Sub
    Private Sub NapDataGridViewbiendong()
        Dim tbl As DataTable
        Dim dbAdapter As SqlDataAdapter
        Dim dbComm As SqlCommand
        dbComm = New SqlCommand("select  noidungbd  From  BIENDONG  WHERE LoaiBDid = '" & CmbLoaiBD.SelectedValue.ToString & "' and noidungbd like N'%" & TextNoiDungBD.Text & "%'" &
                                " and diachixaid = '" & CmbXa.SelectedValue.ToString & "' " &
                                " and diachihuyenid = '" & CmbHuyen.SelectedValue.ToString & "'group by noidungbd ", ob_cnn) '
        dbAdapter = New SqlDataAdapter(dbComm)
        tbl = New DataTable()
        dbAdapter.Fill(tbl)
        DataGridViewNoiDungBienDong.DataSource = tbl
        ob_cnn.Close()
    End Sub
    Private Sub NapVaoTextbox()
        ChuSD1.DataBindings.Clear()
        ChuSD1.DataBindings.Add("Text", DataGridViewTenChu.DataSource, "ChuSD1")
        TextDiaChiTruoc.DataBindings.Clear()
        TextDiaChiTruoc.DataBindings.Add("Text", DataGridViewTenChu.DataSource, "cho_o1")
    End Sub
    Private Sub NapDataGridViewNoiDungBienDong()
        TextNoiDungBD.DataBindings.Clear()
        TextNoiDungBD.DataBindings.Add("text", DataGridViewNoiDungBienDong.DataSource, "noidungbd")
    End Sub

    Private Sub DataGridViewTenChu_CellClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridViewTenChu.CellClick
        NapVaoTextbox()
    End Sub
    Private Sub DataGridViewTenChu_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles DataGridViewTenChu.RowPostPaint
        Dim strRowNumber As String = (e.RowIndex + 1).ToString
        While (strRowNumber.Length < DataGridViewTenChu.RowCount.ToString.Length)
            strRowNumber = "0" & strRowNumber
        End While
        Dim Size As SizeF = e.Graphics.MeasureString(strRowNumber, MyBase.Font)
        If DataGridViewTenChu.RowHeadersWidth < Size.Width + 20 Then DataGridViewTenChu.RowHeadersWidth = Size.Width + 20
        Dim b As Brush = SystemBrushes.ControlText
        e.Graphics.DrawString(strRowNumber, MyBase.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y +
                              ((e.RowBounds.Height - Size.Height) / 2))
    End Sub
    Private Sub TextNoiDungBD_TextChanged(sender As Object, e As EventArgs) Handles TextNoiDungBD.TextChanged
        If TextNoiDungBD.Text = "" Then
            Return
        Else
            NapDataGridViewbiendong()
        End If
    End Sub

    Private Sub DataGridViewNoiDungBienDong_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridViewNoiDungBienDong.CellContentClick
        NapDataGridViewNoiDungBienDong()
    End Sub

    Private Sub DataGridViewNoiDungBienDong_RowPostPaint(sender As Object, e As DataGridViewRowPostPaintEventArgs) Handles DataGridViewNoiDungBienDong.RowPostPaint
        Dim strRowNumber As String = (e.RowIndex + 1).ToString
        While (strRowNumber.Length < DataGridViewNoiDungBienDong.RowCount.ToString.Length)
            strRowNumber = "0" & strRowNumber
        End While
        Dim Size As SizeF = e.Graphics.MeasureString(strRowNumber, MyBase.Font)
        If DataGridViewNoiDungBienDong.RowHeadersWidth < Size.Width + 20 Then DataGridViewNoiDungBienDong.RowHeadersWidth = Size.Width + 20
        Dim b As Brush = SystemBrushes.ControlText
        e.Graphics.DrawString(strRowNumber, MyBase.Font, b, e.RowBounds.Location.X + 15, e.RowBounds.Location.Y +
                              ((e.RowBounds.Height - Size.Height) / 2))
    End Sub


    Private Sub CmbLoaiBD_SelectedIndexChanged(sender As Object, e As EventArgs) Handles CmbLoaiBD.SelectedIndexChanged
        If CmbLoaiBD.Text.Equals("Chuyển đổi quyền sử dụng đất") Then
            TextNoiDungBD.Text = "Chuyển đổi cho "
        ElseIf CmbLoaiBD.Text.Equals("Chuyển mục đích sử dụng đất") Then
            TextNoiDungBD.Text = "Mục đích sử dụng sau khi thay đổi là "
        ElseIf CmbLoaiBD.Text.Equals("Xóa thế chấp, xóa bảo lãnh quyền sử dụng đất") Then
            TextNoiDungBD.Text = "Đã xóa đăng ký thế chấp"
            'ElseIf CmbLoaiBD.Text.Equals("Đính chính thông tin cấp giấy") Then
            '    TextNoiDungBD.Text = "Nội dung về … có sai sót, nay đính chính là "
        ElseIf CmbLoaiBD.Text.Equals("Cấp mới Giấy chứng nhận") Then
            TextNoiDungBD.Text = "Cấp GCNQSDĐ có số phát hành là "
        ElseIf CmbLoaiBD.Text.Equals("Chuyển nhượng quyền sử dụng đất") Then
            TextNoiDungBD.Text = "Chuyển nhượng cho "
        ElseIf CmbLoaiBD.Text.Equals("Cấp đổi, cấp lại Giấy chứng nhận") Then
            TextNoiDungBD.Text = "Cấp lại GCNQSDĐ có số phát hành là "
        ElseIf CmbLoaiBD.Text.Equals("Thừa kế quyền sử dụng đất") Then
            TextNoiDungBD.Text = "Để thừa kế cho "
        ElseIf CmbLoaiBD.Text.Equals("Tặng cho quyền sử dụng đất") Then
            TextNoiDungBD.Text = "Tặng cho cho "
        ElseIf CmbLoaiBD.Text.Equals("Người sử dụng đất đổi tên") Then
            TextNoiDungBD.Text = "Người sử dụng đất được đổi tên là "
        Else
            TextNoiDungBD.Text = ""
        End If

    End Sub
End Class